<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-button class="filter-item" type="primary" v-waves icon="el-icon-plus" @click="handleUpdate(null)">添加</el-button> -->
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="问题" v-model="filters.like$title"></el-input>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" v-if="~accessable.indexOf('commonpage:manage_export')" type="primary" v-waves icon="el-icon-document" @click="exportExcelFile">导出</el-button>
    </div>
    <el-table :data="list" @sort-change="sortChange" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
      style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="80">
      </el-table-column>
      <el-table-column prop="title" align="center" label="页面类型" width="200">
        <template slot-scope="scope">
          <span>{{scope.row.token|token2title}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" align="center" label="创建时间" width="220">
        <template slot-scope="scope">
          <span>{{scope.row.createTime}}</span>
        </template>
      </el-table-column>
  <!--
      <el-table-column prop="modifyTime" align="center" label="修改时间" width="220">
        <template slot-scope="scope">
          <span>{{scope.row.modifyTime}}</span>
        </template>
      </el-table-column>
-->
      <el-table-column align="center" label="操作" width="200" class-name="small-padding">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" v-if="~accessable.indexOf('commonpage:manage_modify')" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" v-if="~accessable.indexOf('commonpage:manage_delete')" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="params.page"
        :page-sizes="[10,20,30,50]" :page-size="params.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="isEditing?'修改':'添加'" :visible.sync="dialogFormVisible">
      <el-form class="dialog-form" :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="120px">

<el-form-item label="页面类型" prop="token">

      {{temp.token|token2title}}

          <!-- <el-select v-model="temp.token" placeholder="请选择">
    <el-option
      v-for="item in tokens"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select> -->

        </el-form-item>
        <!-- <el-form-item label="标题" prop="title">

          <el-input v-model="temp.title" placeholder="页面标题"></el-input>

        </el-form-item> -->

        <el-form-item label="内容" prop="content">

<div class="quill-wrap" style="height:500px;">
            <quill-editor style="height:400px;" v-model="temp.content" ref="myQuillEditor" :options="editorOption">
            </quill-editor>
          </div>

        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" :loading="submiting" @click="submitFormAction">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor, Quill } from 'vue-quill-editor'
import { container, ImageExtend, QuillWatch } from 'quill-image-extend-module'

Quill.register('modules/ImageExtend', ImageExtend)
let tokens = [
  {value: 'aboutus', label: '关于我们'},
  {value: 'agreement', label: '用户协议'},
  {value: 'points', label: '积分规则'},
  {value: 'user', label: '会员说明'},
  {value: 'registration', label: '中油BP会员服务协议'},
  {value: 'privacyAgreement', label: '中油BP隐私政策'},
  {value: 'share_rule', label: '邀请攻略'}
]
export default {
  components: {
    quillEditor
  },
  data () {
    return {
      tokens,
      total: 0,
      temp: {
        id: '',

        token: '',

        content: ''
      },
      editorOption: {
        placeholder: '在这里输入内容',
        modules: {
          ImageExtend: {
            loading: true,
            name: 'file',
            action: this.UPLOAD_URL,
            response: res => {
              return res.info
            }
          },
          toolbar: {
            container: container,
            handlers: {
              image: function () {
                QuillWatch.emit(this.quill.id)
              }
            }
          }
        }
      },
      rules: {
        token: [
          {
            required: true,
            message: '请选择类型',
            trigger: 'change'
          }
        ],
        title: [
          {
            required: true,
            message: '问题不能为空',
            trigger: 'change'
          },

          {
            max: 60,
            message: '不能多余60个字',
            trigger: 'change'
          }
        ],

        content: [
          {
            required: true,
            message: '回答不能为空',
            trigger: 'change'
          }
        ]
      }
    }
  },
  created () {
    this.init('common_page', 1).loadData()
  },
  filters: {
    token2title (token) {
      let rt
      tokens.forEach(el => {
        if (token == el.value) {
          rt = el.label
        }
      })
      return rt
    }
  },
  methods: {
    exportExcelFile () {
      this.exportExcel({
        title: '常见问答',
        fields: [
          {
            field: 'title',
            title: '问题'
          },

          {
            field: 'content',
            title: '回答'
          },

          {
            field: 'createTime',
            title: '创建时间'
          },

          {
            field: 'modifyTime',
            title: '修改时间'
          }
        ]
      }).then(info => {
        this.openWin(info)
      })
    }
  }
}
</script>
